package com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import android.content.Context;
import com.studay.bean.Const;

public class EducationUtil {
	
	/**
	 * get mac key(begin)+Long(mac)+key(end)
	 * 
	 * @param key
	 * @param mac
	 * @return
	 */
	public static String getMAC(String key, String mac) {
		try {
			int hashCode = mac.hashCode();
			int macInt = Math.abs(hashCode);
			StringBuffer sb = new StringBuffer();
			sb.append(key.charAt(0));
			sb.append(key.charAt(key.length() - 1));
			sb.append(macInt);
			return sb.toString();
		} catch (Exception e) {
			LogManager.LogShow(Const.TAG, e.getMessage(), LogManager.ERROR);
			return "";
		}
	}

	/**
	 * read properties
	 * 
	 * @param context
	 * @return
	 */
	public static Properties getProperties(Context context) {
		FileInputStream fin;
		File file = new File("/data/data/identify.conf");
		Properties pro = new Properties();
		if (file.isFile()) {
			try {
				fin = new FileInputStream("/data/data/identify.conf");
				// fin = context.openFileInput("identify.conf");
				pro.load(fin);
			} catch (FileNotFoundException e) {
				LogManager.LogShow(Const.TAG, e.getMessage(), LogManager.ERROR);
			} catch (IOException e) {
				LogManager.LogShow(Const.TAG, e.getMessage(), LogManager.ERROR);
			}
		} else {
			pro = null;
		}
		return pro;
	}

	/**
	 * get ChannelUrl
	 * 
	 * @param properties
	 * @return
	 */
	public static String getChannelUrl(Properties properties) {
		StringBuilder sb = new StringBuilder();
		sb.append(properties.getProperty("ChannelUrl"));
		sb.append(Const.result.getGroup());
		sb.append("/channellist.xml");
		return sb.toString();
	}

	@SuppressWarnings("finally")
	public static int stringToInt(String str) {
		int i = 0;
		try {
			i = Integer.parseInt(str);
		} catch (Exception e) {
			LogManager.LogShow(Const.TAG, e.getMessage(), LogManager.ERROR);
		} finally {
			return i;
		}
	}

	/**
	 * get login url
	 * 
	 * @param properties
	 * @param mac
	 * @return
	 */
	public static String getCheckUserUrl(Properties properties, String mac) {
		StringBuilder sb = new StringBuilder();
		sb.append(properties.getProperty("authUrl").trim());
		sb.append("?");
		sb.append("keyvalue=" + properties.getProperty("keyValue"));
		sb.append("&sn=");
		sb.append(mac);
		return sb.toString();
	}
}